package top.lenjon.lib.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lenjon.lib.entity.UserDetail;
import top.lenjon.lib.model.ResponseModel;
import top.lenjon.lib.service.impl.UserDetailServiceImpl;
import top.lenjon.lib.util.RedisUtil;

/**
 * @author wei
 * @since 2020/11/9 15:12
 */
@RestController
@RequestMapping("/register")
public class RegisterController {
    @Autowired
    UserDetailServiceImpl userService;

    @Autowired
    RedisUtil redisUtil;

    /**
     * 用户注册
     * @param user user
     * @return 注册结果
     */
    @PostMapping("/user")
    public ResponseModel registerUser(@RequestBody UserDetail user) {
        String username = user.getUsername();
        if (userService.getCountByUserName(username) == 0) {
            return ResponseModel.success("注册成功");
        }
        return ResponseModel.error("用户名已存在");
    }

    /**
     * 邮箱验证
     * @param uuid uuid
     * @return ok
     */
    @GetMapping("/confirm")
    public String confirm(String uuid) {
        if (redisUtil.hasKey(uuid)) {
            UserDetail user = (UserDetail) redisUtil.get(uuid);
            user.setType(0);
            user.setEnabled(true);
            userService.save(user);
        }
        return "ok";
    }
}
